function convert(input) { alert('Error. Contact us if this keeps happening.'); document.getElementById('modal-contact').classList.add('show'); } window.ctMaxUploadFiles = 1; var _bwState = { image: null, fileName: 'image', outMime: 'image/png', outExt: '.png', frame: null }; function _bwDetectOutput(blob) { var mime = (blob && blob.type ? blob.type.toLowerCase() : ''); if (mime === 'image/jpeg') { _bwState.outMime = 'image/jpeg'; _bwState.outExt = '.jpg'; } else if (mime === 'image/webp') { _bwState.outMime = 'image/webp'; _bwState.outExt = '.webp'; } else { _bwState.outMime = 'image/png'; _bwState.outExt = '.png'; } } function _bwRenderCanvas(source) { var iw = source.naturalWidth || source.width; var ih = source.naturalHeight || source.height; var canvas = document.createElement('canvas'); canvas.width = iw; canvas.height = ih; var ctx = canvas.getContext('2d'); ctx.drawImage(source, 0, 0, iw, ih); var modeEl = document.getElementById('bw-mode'); var thresholdEl = document.getElementById('bw-threshold'); var mode = modeEl ? modeEl.value : 'dither'; var threshold = thresholdEl ? parseInt(thresholdEl.value, 10) : 128; if (!isFinite(threshold)) threshold = 128; var imageData = ctx.getImageData(0, 0, iw, ih); var data = imageData.data; var pixelCount = iw * ih; if (mode === 'dither') { var gray = new Float32Array(pixelCount); var gi = 0; for (var p = 0; p < data.length; p += 4, gi++) { gray[gi] = 0.299 * data[p] + 0.587 * data[p + 1] + 0.114 * data[p + 2]; } for (var y = 0; y < ih; y++) { for (var x = 0; x < iw; x++) { var idx = y * iw + x; var oldVal = gray[idx]; var newVal = oldVal < 128 ? 0 : 255; var err = oldVal - newVal; gray[idx] = newVal; if (x + 1 < iw) gray[idx + 1] += err * 7 / 16; if (y + 1 < ih) { if (x - 1 >= 0) gray[idx + iw - 1] += err * 3 / 16; gray[idx + iw] += err * 5 / 16; if (x + 1 < iw) gray[idx + iw + 1] += err * 1 / 16; } } } var gi2 = 0; for (var p2 = 0; p2 < data.length; p2 += 4, gi2++) { var v = gray[gi2] < 128 ? 0 : 255; data[p2] = v; data[p2 + 1] = v; data[p2 + 2] = v; } } else { for (var p3 = 0; p3 < data.length; p3 += 4) { var lum = 0.299 * data[p3] + 0.587 * data[p3 + 1] + 0.114 * data[p3 + 2]; var v2 = lum < threshold ? 0 : 255; data[p3] = v2; data[p3 + 1] = v2; data[p3 + 2] = v2; } } ctx.putImageData(imageData, 0, 0); return canvas; } function _bwFitStage() { var stage = document.getElementById('bw-stage'); if (!stage || !_bwState.image) return; var wrap = stage.parentElement; var maxWidth = wrap ? wrap.clientWidth - 28 : 720; if (maxWidth < 200) maxWidth = 720; var maxHeight = Math.min(560, Math.max(280, Math.floor(window.innerHeight * 0.55))); var img = _bwState.image; var scale = Math.min(maxWidth / img.naturalWidth, maxHeight / img.naturalHeight, 1); if (!isFinite(scale) || scale <= 0) scale = 1; stage.width = Math.max(1, Math.round(img.naturalWidth * scale)); stage.height = Math.max(1, Math.round(img.naturalHeight * scale)); } function _bwRedraw() { if (_bwState.frame !== null) return; _bwState.frame = requestAnimationFrame(function() { _bwState.frame = null; var stage = document.getElementById('bw-stage'); if (!stage || !_bwState.image) return; var rendered = _bwRenderCanvas(_bwState.image); var ctx = stage.getContext('2d'); ctx.clearRect(0, 0, stage.width, stage.height); ctx.drawImage(rendered, 0, 0, stage.width, stage.height); }); } function processFile(blob, fileName) { _bwState.fileName = fileName || 'image'; $('#file-drop-zone').addClass('collapsedDropZone'); (function(){ var $b=$('#file-drop-zone .upload-file-button'); var l=$b.data('change-label'); if(l) $b.text(l); })(); _bwDetectOutput(blob); var img = new Image(); img.onload = function() { _bwState.image = img; var ws = document.getElementById('bw-workspace'); if (ws) ws.style.display = 'block'; _bwFitStage(); _bwRedraw(); }; img.onerror = function() { alert('Could not decode this image file.'); }; img.src = URL.createObjectURL(blob); } function _bwApply() { if (!_bwState.image) return; var rendered = _bwRenderCanvas(_bwState.image); var quality = (_bwState.outMime === 'image/jpeg' || _bwState.outMime === 'image/webp') ? 0.95 : undefined; rendered.toBlob(function(out) { if (!out) { alert('Could not encode the output image.'); return; } var base = (_bwState.fileName || 'image').replace(/\.[^.]+$/, ''); add_file_output(URL.createObjectURL(out), base + '-bw' + _bwState.outExt); }, _bwState.outMime, quality); } function _bwInit() { var stage = document.getElementById('bw-stage'); if (!stage || stage._bwInitialized) return; stage._bwInitialized = true; var mode = document.getElementById('bw-mode'); var thresholdRow = document.getElementById('bw-threshold-row'); var threshold = document.getElementById('bw-threshold'); var thresholdLabel = document.getElementById('bw-threshold-label'); if (mode) { mode.addEventListener('change', function() { if (thresholdRow) thresholdRow.style.display = mode.value === 'threshold' ? '' : 'none'; _bwRedraw(); }); } if (threshold && thresholdLabel) { threshold.addEventListener('input', function() { thresholdLabel.textContent = threshold.value; _bwRedraw(); }); } var apply = document.getElementById('bw-apply'); if (apply) apply.addEventListener('click', _bwApply); window.addEventListener('resize', function() { if (!_bwState.image) return; _bwFitStage(); _bwRedraw(); }); } document.addEventListener('DOMContentLoaded', _bwInit); if (document.readyState !== 'loading') _bwInit(); var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }